 
import { inject } from 'vue'
import type { InjectionKey } from 'vue'
import type BaseObject from '../base2/base-object'
import type BaseArray from '../base2/base-array'

/**
 * 获取局部状态，对象，setup 
 * @param id 状态的 ID
 * @returns ID 对应的局部状态
 */
export default function useStoreLocal<T extends object>
  (id: string | symbol)  {
    const key = id as InjectionKey<T>
    const re = inject<T>(key)
    if (typeof re === 'undefined') {
      console.error(`没有找到局部状态：${key}`)
    }
    return re as T
  }

/**
 * 获取局部状态，对象基类 BaseObject
 * @param id 状态的 ID
 * @returns ID 对应的局部状态
 */
export function useStoreLocalObject<T extends object>
  (id: string | symbol): T & BaseObject<T> {

    const key = id as InjectionKey<BaseObject<T> & T>
    const re = inject<T>(key) as T & BaseObject<T>
    if (typeof re === 'undefined') {
      console.error(`没有找到局部状态：${key}`)
    }
    return re 
  }

  /**
 * 获取局部状态，数组，BaseArray
 * @param id 状态的ID
 * @returns 局部状态
 */
export function useStoreLocalArray<T extends object | any>
  (id: string | symbol): BaseArray<T> {
    const key = id as InjectionKey<BaseArray<T> & T>
    const re = inject<T>(key) as BaseArray<T>
    return re 
  }